package ernie.gui;

import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/*
 * StockChooser.java
 *
 * Created on Dec 8, 2011, 8:16:13 PM
 */
/**
 * 
 * @author Filip Lindqvist <buggfille@gmail.com>
 */
public class StockChooser extends javax.swing.JFrame {
    private final BlockingDeque<String> deque;
    private DefaultListModel<String> dlm;
    private final List<String> stocks;

    /** Creates new form StockChooser */
    public StockChooser(List<String> stocks) {
        deque = new LinkedBlockingDeque<String>();
        this.stocks = stocks;

        initComponents();
        setItems(stocks);
        jList1.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    int index = e.getFirstIndex();
                    deque.add(getSelectedStock(index));
                }
            }
        });

    }

    public void setItems(List<String> stocks) {
        initList();
        jList1.setListData(stocks.toArray());
    }

    public void initList() {
        jList1 = new javax.swing.JList();
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jList1);
    }

    private String getSelectedStock(int indexDlm) {
        return stocks.get(indexDlm);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed"
    // desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ernie Graphing Client");

        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jList1);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("My stocks");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(
                                                        jScrollPane1,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        195, Short.MAX_VALUE)
                                                .addComponent(jLabel1))
                                .addContainerGap()));
        layout.setVerticalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel1)
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        278, Short.MAX_VALUE).addContainerGap()));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;

    // End of variables declaration//GEN-END:variables

    public String doStockSelection() {
        setVisible(true);
        try {
            String stock = deque.take();
            setVisible(false);
            dispose();
            return stock;
        } catch (InterruptedException ie) {
            JOptionPane.showMessageDialog(null, ie.toString());
            return null;
        }
    }
}
